<template>
  <div class="index">
    <div class="container">
      <div class="sweiper-box">
        <div class="menu-wrap">
          <div class="menu-item">
            <div class="item-title">手机 电话卡</div>
            <div class="children">
              <div
                v-for="(item, index) in menuList"
                :key="index"
                class="child-father"
              >
                <div class="child" v-for="(sub, j) in item" :key="j">
                  <img :src="sub ? sub.img : '/imgs/item-box-1.png'" alt="" />
                  <span>{{ sub ? sub.name : "小米9" }}</span>
                </div>
              </div>
            </div>
          </div>
          <div class="menu-item">
            <div class="item-title">电视 盒子</div>
          </div>
          <div class="menu-item">
            <div class="item-title">笔记本 平板</div>
          </div>
          <div class="menu-item">
            <div class="item-title">家电 插线板</div>
          </div>
          <div class="menu-item">
            <div class="item-title">出行 穿戴</div>
          </div>
          <div class="menu-item">
            <div class="item-title">手机 电话卡</div>
          </div>
          <div class="menu-item">
            <div class="item-title">只能 路由器</div>
          </div>
          <div class="menu-item">
            <div class="item-title">生活 箱包</div>
          </div>
        </div>
        <swiper ref="mySwiper" :options="swiperOptions">
          <swiper-slide v-for="item in slideList" :key="item.id">
            <a :href="`/#/product/${item.id}`">
              <img :src="item.img" />
            </a>
          </swiper-slide>
          <div class="swiper-pagination" slot="pagination"></div>
          <div class="swiper-button-prev" slot="button-prev"></div>
          <div class="swiper-button-next" slot="button-next"></div>
        </swiper>
      </div>
      <div class="ads-box">
        <img
          v-lazy="item.img"
          alt=""
          v-for="(item, index) in adsList"
          :key="index"
          @click="
            $router.push({
              path: `/product/${item.id}`,
            })
          "
        />
      </div>
      <div class="banner">
        <img v-lazy="'/imgs/banner-1.png'" alt="" />
      </div>
    </div>
    <div class="product-box">
      <div class="container">
        <h2>手机</h2>
        <div class="wrapper">
          <div class="banner-left">
            <a href="/#/product/35">
              <img src="/imgs/mix-alpha.jpg" alt="" />
            </a>
          </div>
          <div class="list-box">
            <div class="list" v-for="(item, i) in phoneList" :key="i">
              <div class="item" v-for="(phone, j) in item" :key="j">
                <span :class="[j % 2 == 0 ? 'new-product' : 'not-new-product']"
                  >新品</span
                >
                <img v-lazy="phone.mainImage" alt="" />
                <h3 class="phone-name">{{ phone.name }}</h3>
                <span class="phone-info">{{ phone.subtitle }}</span>
                <span class="phone-cart" @click="addCart(phone.id)">{{
                  phone.price + " 元"
                }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <ServiceBar />
    <modal
      title="提示"
      sureText="查看购物车"
      btnType="1"
      modalType="middle"
      :showModal="showModal"
      @cancel="showModal = false"
      @submit="goToCart"
    >
      <template v-slot:body>
        <p>商品添加成功！</p>
      </template>
    </modal>
  </div>
</template>

<script>
import ServiceBar from "@/components/ServiceBar";
import { Swiper, SwiperSlide } from "vue-awesome-swiper";
import Modal from "@/components/Modal";
import "swiper/dist/css/swiper.css";
// import 'swiper/css/swiper.css'
export default {
  components: {
    ServiceBar,
    Swiper,
    SwiperSlide,
    Modal,
  },
  data() {
    return {
      swiperOptions: {
        autoplay: true,
        loop: true,
        effect: "cube",
        cubeEffect: {
          shadowOffset: 100,
          shadowScale: 0.6,
        },
        pagination: {
          el: ".swiper-pagination",
          clickable: true,
        },
        navigation: {
          nextEl: ".swiper-button-next",
          prevEl: ".swiper-button-prev",
        },
      },
      slideList: [
        {
          id: "42",
          img: require("../../../public/imgs/slider/slide-1.jpg"),
        },
        {
          id: "45",
          img: require("../../../public/imgs/slider/slide-2.jpg"),
        },
        {
          id: "46",
          img: require("../../../public/imgs/slider/slide-3.jpg"),
        },
        {
          id: "1",
          img: require("../../../public/imgs/slider/slide-4.jpg"),
        },
        {
          id: "2",
          img: require("../../../public/imgs/slider/slide-5.jpg"),
        },
      ],
      menuList: [
        [
          {
            id: 30,
            img: "/imgs/item-box-1.png",
            name: "小米CC9",
          },
          {
            id: 31,
            img: "/imgs/item-box-2.png",
            name: "小米8青春版",
          },
          {
            id: 32,
            img: "/imgs/item-box-3.jpg",
            name: "Redmi K20 Pro",
          },
          {
            id: 33,
            img: "/imgs/item-box-4.jpg",
            name: "移动4G专区",
          },
        ],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
      ],
      adsList: [
        {
          id: 33,
          img: "/imgs/ads/ads-1.png",
        },
        {
          id: 48,
          img: "/imgs/ads/ads-2.jpg",
        },
        {
          id: 45,
          img: "/imgs/ads/ads-3.png",
        },
        {
          id: 47,
          img: "/imgs/ads/ads-4.jpg",
        },
      ],
      phoneList: [],
      showModal: false,
    };
  },
  computed: {
    swiper() {
      return this.$refs.mySwiper.$swiper;
    },
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.axios
        .get("/products", {
          params: {
            categoryId: 100012,
            pageSize: 14,
          },
        })
        .then((res) => {
          res.list = res.list.slice(6, 14);
          this.phoneList = [res.list.slice(0, 4), res.list.slice(4, 8)];
        });
    },
    addCart(id) {
      this.axios
        .post("/carts", {
          productId: id,
          selected: true,
        })
        .then((res) => {
          this.showModal = true;
          this.$store.dispatch("saveCartCount", res.cartTotalQuantity);
        });
    },
    goToCart() {
      this.$router.push("/cart");
    },
  },
};
</script>

<style lang="scss" scoped>
@import "@/assets/scss/config.scss";
@import "@/assets/scss/mixin.scss";
.index {
  .sweiper-box {
    position: relative;
    .swiper-container {
      height: 451px;
      .swiper-button-prev {
        padding-left: 550px;
      }
      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
    }
    .menu-wrap {
      width: 264px;
      height: 451px;
      position: absolute;
      background-color: rgba(92, 92, 94, 0.5);
      top: 0;
      left: 0;
      z-index: 100;
      padding: 26px 0;
      .menu-item {
        .item-title {
          cursor: pointer;
          font-size: 16px;
          color: #ffffff;
          height: 50px;
          line-height: 50px;
          padding: 0 30px 0 30px;
          display: flex;
          justify-content: space-between;
          &::after {
            content: ">";
          }
          &:hover {
            background-color: $colorA;
          }
        }
        &:hover {
          .children {
            display: block;
          }
        }
        .children {
          display: none;
          width: 962px;
          height: 451px;
          position: absolute;
          top: 0;
          left: 264px;
          border: 1px solid #e5e5e5;
          background-color: #ffffff;

          .child-father {
            display: flex;
            padding: 20px 50px;
            @include flex();
            cursor: pointer;
            img {
              width: 42px;
              height: 35px;
              vertical-align: middle;
              margin-right: 15px;
            }
          }
        }
      }
    }
  }
  .ads-box {
    margin: 14px 0 30px 0;
    cursor: pointer;
    @include flex();
    img {
      width: 296px;
      height: 167px;
      object-fit: cover;
    }
  }
  .banner {
    margin-bottom: 50px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .product-box {
    background-color: #f5f5f5;
    padding: 30px 0 50px;
    h2 {
      font-size: $fontF;
      height: 21px;
      line-height: 21px;
      color: $colorB;
      margin-bottom: 20px;
    }
    .wrapper {
      display: flex;
      .list-box {
        .list {
          .item {
            display: flex;
            flex-direction: column;
            img {
              width: 236px;
              height: 195px;
              object-fit: cover;
            }
          }
        }
      }
      .banner-left {
        margin-right: 16px;
        img {
          width: 224px;
          height: 619px;
          object-fit: cover;
        }
      }
      .list-box {
        flex: 1;
        .list {
          margin-bottom: 14px;
          @include flex();
          flex: 4;
          flex-wrap: wrap;
          .item {
            background-color: $colorG;
            width: 236px;
            height: 302px;
            @include flex(center);
            position: relative;
            .new-product {
              position: absolute;
              top: 0;
              background-color: $colorL;
              @include flex(center);
              width: 67px;
              height: 24px;
              font-size: 14px;
              line-height: 24px;
              color: #ffffff;
            }
            .not-new-product {
              display: none;
            }
            img {
              width: 100%;
              height: 195px;
              object-fit: contain;
              margin-top: 24px;
            }
            .phone-info {
              margin: 6px 0 13px;
              color: $colorD;
            }
            .phone-cart {
              margin: 6px 0 13px;
              color: #f20a0a;
              font-weight: 700;
              font-size: $fontJ;
              &::after {
                display: inline-block;
                content: " ";
                background: url("/imgs/icon-cart-hover.png") no-repeat;
                background-size: contain;
                width: 22px;
                height: 22px;
                margin-left: 5px;
                vertical-align: middle;
                cursor: pointer;
              }
            }
          }
        }
      }
    }
  }
}
</style>